package com.sg.business.commons.ui.vault;

import org.eclipse.rap.rwt.RWT;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;

import com.mobnut.design.ICSSConstants;
import com.sg.business.model.Document;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.page.IEditorPageLayoutProvider;
import com.sg.widgets.registry.config.BasicPageConfigurator;
import com.sg.widgets.registry.config.IPageDelegator;

public class DrawingInformationPage implements IPageDelegator,
		IEditorPageLayoutProvider {

	private Browser viewer;
	private Document document;
	private Button switcher;
	private boolean max;
	private Control customerPage;
	private Control body;

	public DrawingInformationPage() {
	}

	@Override
	public Composite createPageContent(IManagedForm mForm, final Composite parent,
			PrimaryObjectEditorInput input, BasicPageConfigurator conf) {
		document = (Document) input.getData();
		Composite container = new Composite(parent, SWT.NONE);
		
		container.setLayout(new FormLayout());
		Composite content = new Composite(container,SWT.BORDER);
		content.setLayout(new FormLayout());
		viewer = new Browser(content, SWT.NONE);
		switcher = new Button(container, SWT.PUSH);
		switcher.setData(RWT.CUSTOM_VARIANT, ICSSConstants.CSS_LEFT_16);
		switcher.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				max = !max;
				if(max){
					switcher.setData(RWT.CUSTOM_VARIANT, ICSSConstants.CSS_RIGHT_16);
				}else{
					switcher.setData(RWT.CUSTOM_VARIANT, ICSSConstants.CSS_LEFT_16);
				}
				layout(body, customerPage);
				body.getParent().layout();
			}
		});
		
		FormData fd = new FormData();
		viewer.setLayoutData(fd);
		fd.top = new FormAttachment();
		fd.left = new FormAttachment();
		fd.right = new FormAttachment(100);
		fd.bottom = new FormAttachment(100);
		
		fd = new FormData();
		switcher.setLayoutData(fd);
		fd.top = new FormAttachment(0, 4);
		fd.left = new FormAttachment(0);
		fd.width = 16;
		fd.height = 16;
		
		fd = new FormData();
		content.setLayoutData(fd);
		fd.left = new FormAttachment(switcher,4);
		fd.top = new FormAttachment();
		fd.right = new FormAttachment(100);
		fd.bottom = new FormAttachment(100);
		
		refresh();
		return container;
	}


	@Override
	public IFormPart getFormPart() {
		return null;
	}

	@Override
	public boolean canRefresh() {
		return true;
	}

	@Override
	public void refresh() {
		String url = document.getPLMViewUrl();
		if (url != null) {
			viewer.setUrl(url);
		}
	}

	@Override
	public boolean createBody() {
		return true;
	}

	@Override
	public IEditorPageLayoutProvider getPageLayout() {
		return this;
	}

	@Override
	public void layout(Control body, Control customerPage) {
		this.body = body;
		this.customerPage = customerPage;
		FormData fd;
		if (!max) {
			fd = new FormData();
			body.setLayoutData(fd);
			fd.top = new FormAttachment();
			fd.left = new FormAttachment(0, 8);
			fd.right = new FormAttachment(30);
			fd.bottom = new FormAttachment(100);

			fd = new FormData();
			customerPage.setLayoutData(fd);
			fd.top = new FormAttachment(0, 7);
			fd.left = new FormAttachment(body, 8);
			fd.bottom = new FormAttachment(100);
			fd.right = new FormAttachment(100, -8);
		}else{
			fd = new FormData();
			body.setLayoutData(fd);
			fd.top = new FormAttachment();
			fd.left = new FormAttachment(0, 8);
			fd.right = new FormAttachment(0);
			fd.bottom = new FormAttachment();
			fd.width = 0;
			
			fd = new FormData();
			customerPage.setLayoutData(fd);
			fd.top = new FormAttachment(0, 7);
			fd.left = new FormAttachment(0, 8);
			fd.bottom = new FormAttachment(100);
			fd.right = new FormAttachment(100, -8);
			
		}
	}

}
